package com.cat.dataStructure;

import java.util.HashMap;
import java.util.HashSet;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/max-number-of-k-sum-pairs/description/
 * @create 2025/8/14 09:10
 * @since JDK17
 */

public class Solution04 {
    static HashMap<Integer, Integer> map = new HashMap<>();
    public int maxOperations(int[] nums, int k) {
        int n = nums.length, ans = 0, cnt;
        map.clear();
        for (int i = 0; i < n; i++) {
            cnt = map.getOrDefault(k - nums[i], 0);
            if (cnt > 0) {  // 将之前的减少一个
                map.put(k - nums[i], cnt - 1);
                ans++;
            } else {    // 将当前的放进去
                map.put(nums[i], map.getOrDefault(nums[i], 0) + 1);
            }
        }
        return ans;
    }
}
